//
//  BTEOrderView.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit

class BTEOrderView: UIView {
    
    var currenIdm: ((Int)->())?
    
    let navScrollView = UIScrollView()
    
    let navStackView = UIStackView()
    
    let listScrollView = UIScrollView()
    
    let listStackView = UIView()
    
    let padding: CGFloat = 10
    
    var displayIDX: Int = 0
    
    let navs = [
        BTELanguageManager.shared.localizedString(forKey:"All"),
        BTELanguageManager.shared.localizedString(forKey:"Apply"),
        BTELanguageManager.shared.localizedString(forKey:"Repayment"),
        BTELanguageManager.shared.localizedString(forKey:"Finished")
    ]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        navScrollView.showsHorizontalScrollIndicator = false
        navScrollView.showsHorizontalScrollIndicator = false
        navScrollView.alwaysBounceHorizontal = true
        addSubview(navScrollView)
        navScrollView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
        }
        
        navStackView.axis = .horizontal
        navStackView.alignment = .fill
        navStackView.distribution = .equalSpacing
        navScrollView.addSubview(navStackView)
        navStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalToSuperview()
        }
   
        updateNavStackView()
        
        listScrollView.delegate = self
        listScrollView.showsHorizontalScrollIndicator = false
        listScrollView.showsHorizontalScrollIndicator = false
        listScrollView.alwaysBounceHorizontal = true
        listScrollView.isPagingEnabled = true
        addSubview(listScrollView)
        listScrollView.snp.makeConstraints { make in
            make.top.equalTo(navScrollView.snp.bottom)
            make.bottom.equalToSuperview().offset(-100)
            make.left.right.equalToSuperview()
        }
        
        listScrollView.addSubview(listStackView)
        listStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.top.left.equalToSuperview()
            make.height.equalToSuperview()
            make.width.equalTo(UIScreen.main.bounds.width*4)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateNavStackView(){
        navs.enumerated().forEach { item in
            let navButton = UIButton.init(title: "\(item.element)", image: UIImage.init(named: ""))
            
            if item.offset == 0 {
                navButton.setTitleColor(.white)
                navButton.setBackgroundColor(UIColor.colorWithHex("#39B4E6")!)
                navButton.layer.borderColor = UIColor.colorWithHex("#6C2724")!.cgColor
                navButton.layer.borderWidth = 2
            }
            
            navButton.addTarget(self, action: #selector(navClickButton(_:)), for: .touchUpInside)
            navButton.roundCorners(.allCorners, radius: 20)
            navButton.setFont(.boldSystemFont(ofSize: 18))
            navButton.setTitleColor(.black)
            navButton.tag = item.offset
            navStackView.addArrangedSubview(navButton)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        navStackView.subviews.enumerated().forEach { item in
            if let button = item.element as? UIButton  {
                let textWidth = navs[item.offset].size(withAttributes: [.font: UIFont.boldSystemFont(ofSize: 18)]).width
                let buttonWidth = textWidth + padding
                button.snp.remakeConstraints { make in
                    if BTELanguageManager.shared.currentLanguage == .YINDU {
                        make.width.equalTo(110)
                    } else {
                        make.width.equalTo(buttonWidth+34)
                    }
                    make.height.equalTo(40)
                }
            }
        }
    }
  
}


extension BTEOrderView {
    
    @objc func navClickButton(_ button: UIButton){
        navStackView.subviews.enumerated().forEach { item in
            if let button = item.element as? UIButton  {
                button.setTitleColor(.black)
                button.setBackgroundColor(.white)
                button.layer.borderColor = UIColor.clear.cgColor
            }
        }
        
        listScrollView.setContentOffset(CGPoint.init(x: button.tag*Int(UIScreen.main.bounds.width), y: 0), animated: false)
        
        displayIDX = button.tag
        
        currenIdm?(button.tag)
        
        button.setTitleColor(.white)
        button.setBackgroundColor(UIColor.colorWithHex("#39B4E6")!)
        button.layer.borderColor = UIColor.colorWithHex("#6C2724")!.cgColor
        button.layer.borderWidth = 2
        
        let buttonFrameInScroll =  button.convert( button.bounds, to: navScrollView)
        navScrollView.scrollRectToVisible(buttonFrameInScroll, animated: true)
    }
    
}


extension BTEOrderView: UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let tag = Int(scrollView.contentOffset.x / UIScreen.main.bounds.width)
        
        listScrollView.setContentOffset(CGPoint.init(x: CGFloat(tag) * UIScreen.main.bounds.width, y: 0), animated: true)
        
        navStackView.subviews.enumerated().forEach { item in
            guard let button = item.element as? UIButton else {
                return
            }
            
            let bu = navStackView.subviews[tag]
            let buttonFrameInScroll =  bu.convert( bu.bounds, to: navScrollView)
            self.navScrollView.scrollRectToVisible(buttonFrameInScroll, animated: true)
            
            button.setTitleColor(.black)
            button.setBackgroundColor(.white)
            button.layer.borderColor = UIColor.clear.cgColor
            
            if item.offset == tag {
                button.setTitleColor(.white)
                button.setBackgroundColor(UIColor.colorWithHex("#39B4E6")!)
                button.layer.borderColor = UIColor.colorWithHex("#6C2724")!.cgColor
                button.layer.borderWidth = 2
            }
            
            displayIDX = tag
            
            currenIdm?(tag)
        
        }
    
    }
}
